package com.fuxue.service.impl;

import com.fuxue.constant.MessageConstant;
import com.fuxue.constant.PasswordConstant;
import com.fuxue.constant.StatusConstant;
import com.fuxue.context.BaseContext;
import com.fuxue.dto.*;
import com.fuxue.entity.AllFiled;
import com.fuxue.entity.Filed;
import com.fuxue.entity.Server;
import com.fuxue.exception.PasswordErrorException;
import com.fuxue.mapper.ServerMapper;
import com.fuxue.service.ServerService;
import com.fuxue.vo.ServerVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import javax.security.auth.login.AccountLockedException;
import javax.security.auth.login.AccountNotFoundException;
import java.lang.reflect.Array;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class ServerSeviceImpl implements ServerService {

    @Autowired
    private ServerMapper serverMapper;

    /**
     * 员工登录
     *
     * @param
     * @return
     */
    public Server login(ServerLoginDTO serverLoginDTO) {
        String servername = serverLoginDTO.getServerName();
        String password = serverLoginDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        Server server = serverMapper.getByUsername(servername);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (server == null) {
            //账号不存在
            try {
                System.out.println("抛出异常");
                throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
            } catch (AccountNotFoundException e) {
                throw new RuntimeException(e);
            }
        }

        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(server.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (server.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            try {
                throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
            } catch (AccountLockedException e) {
                throw new RuntimeException(e);
            }
        }

        //3、返回实体对象
        return server;
    }

    @Override
    @Transactional
    public void save(ServerDTO serverDTO) {

        //对象属性拷贝
        //employee.setUpdateTime(LocalDateTime.now());
        String password = serverDTO.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        serverDTO.setPassword(password);
        serverDTO.setCreatTime(LocalDateTime.now());
        serverMapper.insert(serverDTO);
        Integer id = serverMapper.getIdByHao(serverDTO.getServerHao());
        ServerUPFiledDTO serverUPFiledDTO = new ServerUPFiledDTO();
        serverUPFiledDTO.setServerId(id);
        serverUPFiledDTO.setFileds(serverDTO.getFileds());
        updateWithFiled(serverUPFiledDTO);
    }



    /*
*
* 根据id查询
* */
    @Override
    public ServerVO getById(Integer id) {
        Server server = new Server();
        server = serverMapper.getById(id);
        ArrayList<String> filed = serverMapper.getFiledById(id);
        ServerVO serverVO = new ServerVO();
        BeanUtils.copyProperties(server,serverVO);
        serverVO.setFileds(filed);

        return serverVO;
    }

    @Override
    public void updateWithFiled(ServerUPFiledDTO serverUPFiledDTO) {
        Integer id = serverUPFiledDTO.getServerId();
        serverMapper.deleteFiledById(id);
        List<String>list = serverUPFiledDTO.getFileds();
        if (list != null && list.size() > 0) {

            //向领域表插入n条数据
            serverMapper.insertFiled(list,id);
        }

    }

    @Override
    @Transactional
    public void updateWithName(ServerUpNameDTO serverUpNameDTO) {
        serverMapper.updateNameById(serverUpNameDTO);
        serverMapper.updateQuestionName(serverUpNameDTO);
        serverMapper.updateAnserName(serverUpNameDTO);
    }

    @Override
    public void updateWithPassword(ServerUpPasswordDTO serverUpPasswordDTO) {
        Integer id = serverUpPasswordDTO.getServerId();
        String Oldpassword = serverUpPasswordDTO.getOldpassword();
        String Newpassword = serverUpPasswordDTO.getPassword();
        Server server = serverMapper.getServerById(id);
        if (server == null) {
            //账号不存在
            try {
                throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
            } catch (AccountNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
        Oldpassword = DigestUtils.md5DigestAsHex(Oldpassword.getBytes());
        if (!Oldpassword.equals(server.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        Newpassword = DigestUtils.md5DigestAsHex(Newpassword.getBytes());
        serverMapper.UpdateNewPassword(id,Newpassword);


        if (server.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            try {
                throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
            } catch (AccountLockedException e) {
                throw new RuntimeException(e);
            }
        }



    }

    @Override
    public List<String> getFiled(Integer serverId) {
        List<String>filed = new ArrayList<String>();
        filed = serverMapper.getFiledById(serverId);
        return filed;
    }

    @Override
    @Transactional
    public void  updateWithAdvate(ServerUpAdvateDTO serverUpAdvateDTO) {

        serverMapper.updateAnserAdvatar(serverUpAdvateDTO);
        serverMapper.updateAdvatar(serverUpAdvateDTO);
    }

    @Override
    public List<AllFiled> getAllFiled() {
        List<AllFiled>list = new ArrayList<AllFiled>();
        list = serverMapper.selectAllFiled();
        return list;
    }


}
